package com.walkline.util;

import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableGraphics;

import com.walkline.util.Enumerations.IconSize;

import net.rim.blackberry.api.mail.Message.Icons;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.JPEGEncodedImage;
import net.rim.device.api.system.PNGEncodedImage;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.Dialog;

public class Function
{
    public static void errorDialog(final String message)
    {
        UiApplication.getUiApplication().invokeAndWait(new Runnable()
        {
            public void run()
            {
                Dialog.alert(message);
            } 
        });
    }

	public static PNGEncodedImage GetPNGImage(SVGImage image, int size)
    {
        ScalableGraphics TempScalableGraphics;
        Bitmap bitmap;
        Graphics TempGraphics;
        int iconSize = IconSize.choicesIconSize[size];
        int data[]=new int[iconSize * iconSize];

        bitmap = new Bitmap(iconSize, iconSize);
        bitmap.setARGB(data, 0, iconSize, 0, 0, iconSize, iconSize);

        TempGraphics = Graphics.create(bitmap);
        TempScalableGraphics = ScalableGraphics.createInstance();

        image.setViewportWidth(iconSize);
        image.setViewportHeight(iconSize);

        TempScalableGraphics.bindTarget(TempGraphics);
        TempScalableGraphics.render(0, 0, image);
        TempScalableGraphics.releaseTarget();

        return PNGEncodedImage.encode(bitmap);
    }

	public static Bitmap getUpdateHomeScreenIcon(Bitmap bmp)  
    {    
        int width = bmp.getWidth();    
        int height = bmp.getHeight();   
        int data[]=new int[width*height];  
        Bitmap iconBmp = new Bitmap(width, height);  
  
        iconBmp.setARGB(data, 0, width, 0,0,width,height);  
        Graphics g= Graphics.create(iconBmp);  
        XYRect rect = new XYRect(0, 0, width, height);  
        g.drawBitmap(rect, bmp, 0, 0);  

        return iconBmp;  
    }  
}